package com.mall.admin.service.goods;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mall.common.annotation.Bean;
import com.mall.common.model.goods.GoodsProperty;
import com.mall.common.model.goods.GoodsPropertyValue;
import com.mall.common.service.base.BaseService;
import com.mall.common.service.base.DefaultResult;
import com.mall.common.service.base.Result;
import com.mall.common.util.ResultCode;
import com.mall.common.util.StrKit;

/**
 * 商品属性管理业务层
 * @author zengjintao
 * @version 1.0
 * @createTime 2018年8月5日下午12:16:04
 */
@Bean
public class GoodsPropertysService extends BaseService{

	private static final Log LOG = Log.getLog(GoodsPropertysService.class);
	
	/**
	 * 属性列表
	 * @param pageSize
	 * @param pageNumber
	 * @param condition
	 * @return
	 */
	public Result list(int pageSize,int pageNumber,Kv condition){
		Result result = new DefaultResult();
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS);
		try {
			StringBuilder context = new StringBuilder(" from goods_property where 1=1");
			List<Object> params = new ArrayList<Object>();
			//名称搜索
			if(StrKit.isNotEmpty(condition.getStr("name"))){
				context.append(" and name like ?");
				params.add("%"+ condition.getStr("name") +"%");
			}
			Page<GoodsProperty> page = GoodsProperty.dao.paginate(pageNumber, pageSize, "select *", context.toString(),params.toArray());
			result.setDefaultModel(page);
		} catch (Exception e) {
			resultCode = new ResultCode(ResultCode.FAIL);
			LOG.error("查询异常",e);
		}
		result.setResultCode(resultCode);
		return result;
	}
	
	/**
	 * 添加属性值
	 * @param goodsPropertyValue
	 * @return
	 */
	public ResultCode createPropertyValue(GoodsPropertyValue goodsPropertyValue){
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS, "添加成功");
		try {
			goodsPropertyValue.save();
		} catch (Exception e) {
			resultCode = new ResultCode(ResultCode.FAIL, "添加成功");
			LOG.error("操作异常",e);
		}
		return resultCode;
	}
	
	/**
	 * 添加属性
	 * @param goodsPropertyValue
	 * @return
	 */
	public ResultCode createProperty(GoodsProperty goodsProperty){
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS, "添加成功");
		try {
			goodsProperty.save();
		} catch (Exception e) {
			resultCode = new ResultCode(ResultCode.FAIL, "添加成功");
			LOG.error("操作异常",e);
		}
		return resultCode;
	}
	
	/**
	 * 删除属性
	 * @param id
	 * @return
	 */
	@Before(Tx.class)
	public ResultCode deleteById(int id){
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS, "删除成功");
		try {
			GoodsProperty.dao.deleteById(id);
			//删除属性相关关联
			Db.update("delete from goods_property_value where goods_property_id = ?",id);
		} catch (Exception e) {
			resultCode = new ResultCode(ResultCode.FAIL, "删除失败");
			LOG.error("操作异常",e);
		}
		return resultCode;
	}
	
	/**
	 * 更新属性
	 * @param id
	 * @return
	 */
	public ResultCode update(GoodsProperty goodsProperty){
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS, "更新成功");
		try {
			goodsProperty.update();
		} catch (Exception e) {
			resultCode = new ResultCode(ResultCode.FAIL, "更新失败");
			LOG.error("操作异常",e);
		}
		return resultCode;
	}
	
	/**
	 * 删除属性值
	 * @param id
	 * @return
	 */
	public ResultCode deletePropertyValue(int id){
		ResultCode resultCode = new ResultCode(ResultCode.SUCCESS, "更新成功");
		try {
			GoodsPropertyValue.dao.deleteById(id);
		} catch (Exception e) {
			resultCode = new ResultCode(ResultCode.FAIL, "更新失败");
			LOG.error("操作异常",e);
		}
		return resultCode;
	}
	
	
	/**
	 * 查找属性值列表
	 * @param propertyId
	 * @return
	 */
	public List<GoodsPropertyValue> findByProperty(int propertyId){
		return GoodsPropertyValue.dao.find("select * from goods_property_value where goods_property_id = ?",
				propertyId);
	}
}
